﻿using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Gentings.AspNetCore.TagHelpers.Bootstraps
{
    /// <summary>
    /// Offcanvas标签。
    /// </summary>
    [HtmlTargetElement("gt:offcanvas")]
    public class OffcanvasTagHelper : TagHelperBase
    {
        /// <summary>
        /// 标题。
        /// </summary>
        public string? Title { get; set; }

        /// <summary>
        /// 位置。
        /// </summary>
        public OffcanvasDirection Direction { get; set; } = OffcanvasTagHelper.OffcanvasDirection.End;

        /// <summary>
        /// 是否包含关闭按钮。
        /// </summary>
        public bool Close { get; set; } = true;

        /// <summary>
        /// 位置方向。
        /// </summary>
        public enum OffcanvasDirection
        {
            /// <summary>
            /// 左方。
            /// </summary>
            Start,
            /// <summary>
            /// 右方。
            /// </summary>
            End,
            /// <summary>
            /// 上方。
            /// </summary>
            Top,
            /// <summary>
            /// 下方。
            /// </summary>
            Bottom,
        }

        /// <summary>
        /// 异步访问并呈现当前标签实例。
        /// </summary>
        /// <param name="context">当前HTML标签上下文，包含当前HTML相关信息。</param>
        /// <param name="output">当前标签输出实例，用于呈现标签相关信息。</param>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var content = await output.GetChildContentAsync();
            output.Process("div", builder =>
            {
                builder.AddCssClass($"offcanvas offcanvas-{Direction.ToLowerString()}");
                builder.AppendHtml("div", header =>
                {
                    header.AddCssClass("offcanvas-header");
                    header.AppendHtml("h3", h3 => h3.AppendHtml(Title!));
                    header.AppendHtml("button", btn =>
                    {
                        btn.MergeAttribute("type", "button");
                        btn.AddCssClass("btn-close text-reset");
                        btn.MergeAttribute("data-bs-dismiss", "offcanvas");
                    });
                });
                builder.AppendHtml("div", body =>
                {
                    body.AddCssClass("offcanvas-body");
                    body.AppendHtml(content);
                });
            });
        }
    }
}
